﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Recursive
{

    /// <summary>
    /// 递归算法
    /// </summary>

    internal class Program
    {
        private static void Main(string[] args)
        {


            Console.WriteLine(test4());
            Console.Read();
        }


        ///// 要求输出 1,2,3,5,8,13,21,34,55,89//普通写法
        private static string test1()
        {

            int[] cSum = new int[10];
            string sSum = "";
            for (int i = 0; i < cSum.Length; i++)
            {
                if (i == 0)
                {
                    cSum[i] = 1;
                    sSum = "" + cSum[i];
                }
                else if (i == 1)
                {
                    cSum[i] = cSum[i - 1] + cSum[i - 1];
                    sSum = sSum + ',' + cSum[i];
                }
                else
                {
                    cSum[i] = cSum[i - 1] + cSum[i - 2];
                    sSum = sSum + "," + cSum[i];

                }
            } return sSum;


        }

        public static string test2()
        {
            int[] cSum = new int[10];
            string sSum = string.Empty;
            for (int j = 0; j < cSum.Length; j++)
            {
                cSum[j] = process(j);
                if (sSum != "")
                {
                    sSum = sSum + ',';
                }
                sSum += cSum[j];
            }
            return sSum;
        }
        /// <summary>
        /// 重构使用递归处理
        /// </summary>
        /// <returns></returns>
        private static int process(int i)
        {
            int s;
            if (i == 0 || i == 1)
            {
                s = i + 1;
            }
            else
            {
                s = process(i - 1) + process(i - 2);
            }
            return s;
        }
        //下面留个题目，要求输出:1,1,2,3,5,8,13,21,34,55  
        public static string test3()
        {
            int[] cSum = new int[10];
            string sSum = string.Empty;
            for (int i = 0; i < cSum.Length; i++)
            {
                if (i == 0 || i == 1)
                {
                    cSum[i] = 1 + cSum[i];
                    if (sSum != "")
                    {
                        sSum += ",";
                    }
                    sSum += cSum[i];
                }
                else
                {
                    cSum[i] = cSum[i - 1] + cSum[i - 2];
                    if (sSum != "")
                    {
                        sSum += ",";
                    }
                    sSum += cSum[i];
                }

            }
            return sSum;
        }


        //用递归实现

        public static string test4()
        {
            int[] cSum = new int[10];
            string sSum = string.Empty;
            for (int i = 0; i < cSum.Length; i++)
            {
                cSum[i] = process2(i);
                sSum += cSum[i];
            }
            return sSum;
        }

        public static int process2(int i)
        {
            if (i == 0 || i == 1)
            {
                return 1;
            }
            else
            {
                return process2(i - 1) + process2(i - 2);

            }



        }
    }
}

